//
//  CertificationTableView.swift
//  MT
//
//  Created by 陈悦 on 2020/5/7.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory
import Foundation

@objc
protocol CertificationTableViewTask: NSObjectProtocol {
    @objc optional func certificationTableView(_ tableView: CertificationTableView, willSubmitInfosWithRealName realname: String, idNumber: String)
}

class CertificationTableView: UITableView {
    
    /// 认证代理
    weak var verificationTask: CertificationTableViewTask?
    
    /// 下一步 FooterView
    var footerView: MTInfoVerificationTableFooterView?
    
    
    /// 列表控制数组
    private var infoTypes: [UserProfileInfos] = [.realName, .idNumber,.idNumberImg]
    
    /// 真实姓名
    var realName: String?
    /// 身份证号
    var idNumber: String?
    /// 身份证正面照片
    var idImgFront: MTAsset? {
        didSet {
           reloadData()
        }
    }
    
    /// 身份证反面照片
    var idImgBack: MTAsset? {
        didSet {
            reloadData()
        }
    }
    
    init(frame: CGRect) {
        super.init(frame: frame, style: .grouped)
        setup()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------ 初始化 ----------------------
extension CertificationTableView {
    
    private func setup() {
        backgroundColor = UIColor(hex:0xf6f6f6)
        delegate = self
        dataSource = self
        separatorStyle = .none
        keyboardDismissMode = .onDrag
        contentInset = UIEdgeInsets(top: 1, left: 0, bottom: 0, right: 0)
        
        for type in infoTypes {
            register(type.cellClass, forCellReuseIdentifier: String(describing: type.cellClass))
        }
    }
    
}

// MARK: ------------------ 所有代理 ----------------------
extension CertificationTableView: UITableViewDelegate {
    
}

// MARK: ------------------ UI ----------------------
extension CertificationTableView: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return infoTypes.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return infoTypes[indexPath.row].cellHeight
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let type = infoTypes[indexPath.row]
        let c = tableView.dequeueReusableCell(withIdentifier: String(describing: type.cellClass), for: indexPath)

        switch type {
        case .realName,.idNumber:
            guard let cell = c as? MTInfoVerificationInputCell else {
                break
            }
            cell.delegate = self
            cell.type = type
            cell.screenMode = 1
            cell.textFieldTextAlignment = .right
            switch type {
            case .realName:
                cell.content = realName
                break
            case .idNumber:
                cell.content = idNumber
                break
            default:
                break
            }
            return cell
        case .idNumberImg:
            guard let cell = c as? MTCertificationUploadCell else {
                break
            }
            cell.type = type
            cell.frontAsset = idImgFront
            cell.backAsset = idImgBack
//            cell.screenMode = 1
            return cell
            
        default: break
        }
        
        return c
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.leastNonzeroMagnitude
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if section == 0 {
            return 140
        }
        return CGFloat.leastNonzeroMagnitude
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        if section == 0 {
            let v = MTInfoVerificationTableFooterView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 140))
            v.delegate = self
            v.buttonStr = "提交认证"
            self.footerView = v
            return v
        }
        return nil
    }
    
}

// MARK: ------------------ input delegate ----------------------
extension CertificationTableView: MTInfoVerificationInputCellDelegate {
    
    func inputCell(_ cell: BaseMTInfoVerificationCell, infoType: UserProfileInfos, didInputText text: String?) {
        switch infoType {
        case .realName:
            self.realName = text
            break
        case .idNumber:
            self.idNumber = text
            break
        default:
            break
        }
    }
    
}

// MARK: ------------------ FooterView 代理回调 ----------------------
extension CertificationTableView: MTInfoVerificationTableFooterViewDelegate {

    func tableFooterViewDidClickButton(_ footerView: MTInfoVerificationTableFooterView) {
        if realName?.count ?? 0 <= 0 {
            AlertManager.presentAlertView(message: "姓名不能为空", duration: 1)
            return
        }
        
        if idNumber?.count ?? 0 <= 0 || !(idNumber?.isTrueIDNumber())! {
            AlertManager.presentAlertView(message: "身份证号不合法", duration: 1)
            return
        }
        
        if let verificationTask = verificationTask, let realName = realName, let idNum = idNumber {
            verificationTask.certificationTableView?(self, willSubmitInfosWithRealName: realName, idNumber: idNum)
        }
    }

}
